class Product
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Taggable
  include Mongoid::Attributes::Dynamic

  belongs_to :state

  field :tags_array_size, type: Integer, default: 0 #活动标签数量
  field :off_promotion_subject_product_id, type: String
  field :sec_kill_product_id, type: String
  field :full_reduction_id, type: String
  field :promotion_discount_id, type: String
  field :gift_full_reduction_ids, type: Array, default: []
  field :coupon_id, type: String
  field :title

  field :activity_title

  field :brand #品牌
  field :specification #规格
  field :origin #产地
  field :manufacturer #制造商
  field :type #种类
  field :scent #香型

  field :qrcode
  field :qrcode_suffix

  field :avatar
  field :thumb
  field :mains, type: Array, default: [] #主图
  field :descs, type: Array, default: [] #详情图
  field :desc

  field :mobile_category_name, default: '其他' #类别名称
  field :mobile_category_num, type: Integer, default: 0 #类别排序号

  field :purchasePrice, type: Float, default: 0 #进价
  field :price, type: Float, default: 0 #手机专享价
  field :marketPrice, type: Float, default: 0 #市场价


  field :current_price, type: Float, default: 0 #当前价格 可用于排序
  field :integral, type: Integer, default: 1 #积分
  field :stock, type: Integer #库存数量

  field :sale_count, type: Integer, default: 0 #销量


  field :panic_price, type: Float, default: 0 #活动价格
  field :panic_quantity, type: Integer, default: 0 #活动数量
  field :panic_sale_count, type: Integer, default: 0 #活动销量


  #七月份APP升级版本暂用字段，以后可删除
  field :sec_kill_price, type: Float #秒杀价格


  field :alarm_stock, type: Integer, default: 10 # 预警库存数量

  field :num, type: Integer, default: 0 #排序号
  field :hot_num, type: Integer, default: 0 #热销排序号

  field :exposure_num, type: Integer, default: 0 #商品的曝光数:商品被展示次数
  field :exposure_attrive_num, type: Integer, default: 0 #商品的曝光到达数:商品被展示后，用户点击访问的次数
  field :may_use_integral, type: Boolean, default: true #是否可以使用积分购买
  field :is_jxc, type: Boolean, default: false #进销存是否显示


  field :comment_count, type: Integer, default: 0 #评论数

  attr_accessor :pid, :userinfo_id, :product_before, :product_after

  attr_readonly *fields.keys

  before_save :check_valid
  after_save :set_current_price, :set_keywords

  def avatar_url
    self.avatar.present? ? self.avatar : 'missing.png'
  end

  #商品图片路径
  def main_url
    self.mains.present? ? self.mains[0] : 'missing.png'
  end

  def thumb_url
    self.thumb.present? ? self.thumb : 'missing.png'
  end

  def desc_url
    self.descs.present? ? self.descs[0] : 'missing.png'
  end

  def category_name
    mobile_category_name.present? ? mobile_category_name : '其他'
  end

  def self.shop_id(id)
    @userinfo_id = id
    with(collection: "products#{id}")
  end

  @userinfo

  def shop_id(id)
    @userinfo_id = id
    @userinfo=Userinfo.find(id)
    with(collection: "products#{id}")
  end

  def self.shop(user)
    @userinfo_id = user.userinfo.id
    @userinfo=user.userinfo
    with(collection: user.shop_id) if user.present?
  end

  def self.get_dictionary_fields
    ['type', 'scent', 'brand', 'specification', 'origin', 'manufacturer']
  end

  def shop(user)
    @userinfo_id = user.userinfo.id
    @userinfo=user.userinfo
    with(collection: user.shop_id) if user.present?
  end


  def current_title

    self.activity_title.present? ? self.activity_title : self.title
  end

end


